package handlers

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
	"vehi-info-sys/cache"
	"vehi-info-sys/models"
)

func Add(c *gin.Context) {
	carNo := c.PostForm("car_no")
	carName := c.PostForm("car_name")
	carCode := c.PostForm("car_code")
	tid, _ := strconv.Atoi(c.PostForm("tid"))

	if carNo == "" || carName == "" || carCode == "" || tid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}

	data := models.Car{
		CarNo:   carNo,
		CarName: carName,
		CarCode: carCode,
		Tid:     tid,
	}

	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func CarTypeAdd(c *gin.Context) {
	typeNo := c.PostForm("type_no")
	typeName := c.PostForm("type_name")
	typeContent := c.PostForm("type_content")

	if typeNo == "" || typeName == "" || typeContent == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}

	data := models.CarType{
		TypeNo:      typeNo,
		TypeName:    typeName,
		TypeContent: typeContent,
	}

	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func CarTypeList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size
	/*
		SELECT
		car_types.id as TypeId,
		car_types.type_no as TypeNo,
		car_types.type_name as TypeName,
		car_types.type_content as TypeContent,
		cars.car_no as CarNo,
		cars.car_name as CarName,
		cars.car_code as CarCode
		FROM
		car_types
		LEFT JOIN cars ON cars.tid = car_types.id
		WHERE car_types.id=1
	*/

	var types []models.CarTypeList
	redis_key := "type_list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("car_types").
			Joins("LEFT JOIN cars ON cars.tid = car_types.id").
			Select(
				"car_types.id as TypeId",
				"car_types.type_no as TypeNo",
				"car_types.type_name as TypeName",
				"car_types.type_content as TypeContent",
				"cars.car_no as CarNo",
				"cars.car_name as CarName",
				"cars.car_code as CarCode",
			).
			Offset(offset).
			Limit(size).
			Debug().
			Find(&types)

		jsonData, _ := json.Marshal(types)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &types)
	}

	var count int64
	models.DB.Model(&models.CarType{}).Count(&count)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{
			"count": count,
			"types": types,
		},
	})
	return

}

func CarTypeDetail(c *gin.Context) {
	id := c.Query("id")
	var types []models.CarTypeList
	redis_key := "type_detail" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("car_types").
			Joins("LEFT JOIN cars ON cars.tid = car_types.id").
			Select(
				"car_types.id as TypeId",
				"car_types.type_no as TypeNo",
				"car_types.type_name as TypeName",
				"car_types.type_content as TypeContent",
				"cars.car_no as CarNo",
				"cars.car_name as CarName",
				"cars.car_code as CarCode",
			).
			Where("car_types.id=?", id).
			Debug().
			Find(&types)

		jsonData, _ := json.Marshal(types)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &types)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": types,
	})
	return
}
